package com.csii.modules.online.cgform.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import java.lang.invoke.SerializedLambda;
import java.util.Arrays;
import java.util.List;
import javax.servlet.http.HttpServletRequest;

import com.csii.core.api.Result;
import com.csii.core.system.query.QueryGenerator;
import com.csii.modules.online.cgform.entity.OnlCgformField;
import com.csii.modules.online.cgform.entity.OnlCgformHead;
import com.csii.modules.online.cgform.service.IOnlCgformFieldService;
import com.csii.modules.online.cgform.service.IOnlCgformHeadService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController("onlCgformFieldController")
@RequestMapping({"/online/cgform/field"})
public class CgformFieldController {
   private static final Logger a = LoggerFactory.getLogger(CgformFieldController.class);
   @Autowired
   private IOnlCgformHeadService onlCgformHeadService;
   @Autowired
   private IOnlCgformFieldService onlCgformFieldService;

   @GetMapping({"/listByHeadCode"})
   public Result<?> listByHeadCode(@RequestParam("headCode") String headCode) {
      LambdaQueryWrapper<OnlCgformHead> wrapper = new LambdaQueryWrapper();
	   wrapper.eq(OnlCgformHead::getTableName, headCode);
      OnlCgformHead one = (OnlCgformHead)this.onlCgformHeadService.getOne(wrapper);
      return one == null ? Result.error("不存在的code") : this.listByHeadId(one.getId());
   }

	/**
	 *根据headid查询
	 * @param headId
	 * @return
	 */
	@GetMapping({"/listByHeadId"})
	public Result<?> listByHeadId(@RequestParam("headId") String headId) {
		QueryWrapper queryWrapper = new QueryWrapper();
		//查询条件
		queryWrapper.eq("cgform_head_id", headId);
		queryWrapper.orderByAsc("order_num");

		List list = this.onlCgformFieldService.list(queryWrapper);
		return Result.ok(list);
	}

	/**
	 *分页，列表
	 * @param field
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
   @GetMapping({"/list"})
   public Result<IPage<OnlCgformField>> list(OnlCgformField field, @RequestParam(name = "pageNo",defaultValue = "1") Integer pageNo, @RequestParam(name = "pageSize",defaultValue = "10") Integer pageSize, HttpServletRequest req) {
      Result result = new Result();
      QueryWrapper wrapper = QueryGenerator.initQueryWrapper(field, req.getParameterMap());
      Page page = new Page((long)pageNo, (long)pageSize);
      IPage iPage = this.onlCgformFieldService.page(page, wrapper);
	   result.setSuccess(true);
	   result.setResult(iPage);
      return result;
   }

   @PostMapping({"/add"})
   public Result<OnlCgformField> add(@RequestBody OnlCgformField var1) {
      Result var2 = new Result();

      try {
         this.onlCgformFieldService.save(var1);
         var2.success("添加成功！");
      } catch (Exception var4) {
         a.error(var4.getMessage(), var4);
         var2.error500("操作失败");
      }

      return var2;
   }

   @PutMapping({"/edit"})
   public Result<OnlCgformField> edit(@RequestBody OnlCgformField var1) {
      Result var2 = new Result();
      OnlCgformField var3 = (OnlCgformField)this.onlCgformFieldService.getById(var1.getId());
      if (var3 == null) {
         var2.error500("未找到对应实体");
      } else {
         boolean var4 = this.onlCgformFieldService.updateById(var1);
         if (var4) {
            var2.success("修改成功!");
         }
      }

      return var2;
   }

   @DeleteMapping({"/delete"})
   public Result<OnlCgformField> delete(@RequestParam(name = "id",required = true) String var1) {
      Result var2 = new Result();
      OnlCgformField var3 = (OnlCgformField)this.onlCgformFieldService.getById(var1);
      if (var3 == null) {
         var2.error500("未找到对应实体");
      } else {
         boolean var4 = this.onlCgformFieldService.removeById(var1);
         if (var4) {
            var2.success("删除成功!");
         }
      }

      return var2;
   }

   @DeleteMapping({"/deleteBatch"})
   public Result<OnlCgformField> deleteBatch(@RequestParam(name = "ids",required = true) String var1) {
      Result var2 = new Result();
      if (var1 != null && !"".equals(var1.trim())) {
         this.onlCgformFieldService.removeByIds(Arrays.asList(var1.split(",")));
         var2.success("删除成功!");
      } else {
         var2.error500("参数不识别！");
      }

      return var2;
   }

   @GetMapping({"/queryById"})
   public Result<OnlCgformField> queryById(@RequestParam(name = "id",required = true) String var1) {
      Result var2 = new Result();
      OnlCgformField var3 = (OnlCgformField)this.onlCgformFieldService.getById(var1);
      if (var3 == null) {
         var2.error500("未找到对应实体");
      } else {
         var2.setResult(var3);
         var2.setSuccess(true);
      }

      return var2;
   }
}
